
package com.echo.sdk.im.model;

import com.echo.sdk.im.constant.CIMConstant;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import lombok.Data;

import java.io.Serializable;
import java.net.SocketAddress;

/**
 * @author chentl
 * @version V1.0
 * @Project IM-SDK
 * @Title CIMSession.java
 * @Description IoSession包装类
 * @Package com.whdsj.sdk.im.server.model
 * @date 2020/9/7 8:05 下午
 */
@Data
public class CIMSession implements Serializable {

    private transient static final long serialVersionUID = 1L;

    private transient Channel session;

    /**
     * 数据库主键ID
     */
    private Long id;

    /**
     * session绑定的用户id
     */
    private String userId;
    /**
     * session绑定的账号名称
     */
    private String account;

    /**
     * session在本台服务器上的ID
     */
    private String nid;

    /**
     * session绑定的服务器IP
     */
    private String host;
    /**
     * session绑定的服务器端口
     */
    private int port;

    /**
     * 终端设备类型
     */
    private String channel;

    /**
     * 终端设备型号
     */
    private String device;

    /**
     * 终端设备版本号
     */
    private String osVersion;


    /**
     * 登录时间
     */
    private Long bindTime;


    public CIMSession(Channel session) {
        this.session = session;
        this.nid = session.id().asShortText();
    }


    public CIMSession() {

    }

    public boolean isConnected() {
        return (session != null && session.isActive());
    }

    public void closeOnFlush() {
        if (session != null) {
            session.close();
        }
    }

    /**
     * @return void
     * @author chentl
     * @description 发送消息
     * @date 2020/9/6 3:53 下午
     * @params [data]
     */
    public void write(CIMMsg data) {
        if (session == null || !session.isActive()) {
            return;
        }
        session.writeAndFlush(new TextWebSocketFrame(data.toJsonString()));
    }


    public void setSession(Channel session) {
        this.session = session;
    }


    public void setUserId(String userId) {
        this.userId = userId;

        setAttribute(CIMConstant.KEY_ACCOUNT, userId);
    }


    public Channel getSession() {
        return session;
    }

    public void setAttribute(String key, Object value) {
        if (session != null) {
            session.attr(AttributeKey.valueOf(key)).set(value);
        }
    }

    public boolean containsAttribute(String key) {
        if (session != null) {
            return session.hasAttr(AttributeKey.valueOf(key));
        }
        return false;
    }

    public Object getAttribute(String key) {
        if (session != null) {
            return session.attr(AttributeKey.valueOf(key)).get();
        }
        return null;
    }

    public void removeAttribute(String key) {
        if (session != null) {
            session.attr(AttributeKey.valueOf(key)).set(null);
        }
    }

    public SocketAddress getRemoteAddress() {
        if (session != null) {
            return session.remoteAddress();
        }
        return null;
    }


    @Override
    public int hashCode() {
        return getClass().hashCode();
    }


}
